import 'dart:async';

import 'package:flutter/material.dart';
import "package:get/get.dart";

import '../common.dart';

final RxBool _isObscured = true.obs;
final RxBool _isObscureds = true.obs;

class ChangePassword extends StatefulWidget {
  const ChangePassword({super.key});

  @override
  State<ChangePassword> createState() => _SettingPageState();
}

late TextEditingController code;
late TextEditingController phone;
late TextEditingController controller;
late TextEditingController controller1;
RxInt codetext = 60.obs;
RxBool butdisabled = false.obs;

RxBool valgroup = true.obs;

class _SettingPageState extends State<ChangePassword> {
  final type = Get.parameters['type'];
  final phones = myget('phone');

  @override
  void initState() {
    super.initState();

    code = TextEditingController();
    phone = TextEditingController(text: '$phones');
    controller = TextEditingController(text: '');
    controller1 = TextEditingController(text: '');
  }

  @override
  void dispose() {
    code.dispose();
    phone.dispose();
    controller.dispose();
    controller1.dispose();

    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        backgroundColor: Color(0xffffffff),
        leadingWidth: 40,
        leading: buildBackButton(context),
      ),
      body: Container(
        margin: EdgeInsetsDirectional.symmetric(horizontal: 15),
        child: type == '1' || type == '3' ? setPass(type) : bindEmail(),
      ),
    );
  }
}

Widget bindEmail() {
  return Column(
    crossAxisAlignment: CrossAxisAlignment.start,
    children: [
      Text(
        '绑定电子邮箱',
        style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
      ),
      SizedBox(height: 5),
      Text('验证码发送到您的邮箱，请输入验证码绑定该邮箱'),
      Container(
        margin: EdgeInsets.only(top: 30),
        padding: EdgeInsets.only(left: 20),
        decoration: BoxDecoration(
          color: Color(0xfff5f5f5),
          borderRadius: BorderRadius.circular(30.0),
          border: Border.all(color: Color(0xffffffff)),
        ),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Expanded(
              child: TextField(
                enabled:false,
                style: TextStyle(
                  fontSize: 12.0, // 字体大小
                ),
                decoration: InputDecoration(
                  hintText: '请输入号码',
                  border: InputBorder.none,
                  hintStyle: TextStyle(fontSize: 12, color: Color(0xff999999)),
                ),
              ),
            ),
          ],
        ),
      ),
      Container(
          margin: EdgeInsets.symmetric(horizontal: 30, vertical: 10),
          decoration: BoxDecoration(
              border: Border(bottom: BorderSide(color: Color(0XFFDDDDDD)))),
          child: Row(
            children: [
              Expanded(
                child: TextField(
                  controller: code,
                  style: TextStyle(fontSize: 14),
                  decoration: InputDecoration(
                    isCollapsed: true, //设置高度
                    contentPadding: EdgeInsets.all(15),
                    hintStyle: TextStyle(color: Color(0xff999999)),
                    border: InputBorder.none, // 隐藏输入框默认边框
                    hintText: '请输入验证码', // 提示文本
                  ),
                ),
              ),
              GestureDetector(
                onTap: () {
                  if (phone.text.isNotEmpty) {
                    if (butdisabled.value) {
                      return;
                    }
                    postData(url: '/phone-code',data:  {
                      "phone": code.text,
                      "value": "login",
                    }).then((res) {
                      if (res != null) {
                        showTost('发送成功');
                        butdisabled.value = true;
                        codetext.value = 60;

                        Timer.periodic(Duration(seconds: 1), (timer) {
                          codetext.value--;
                          if (codetext.value < 1) {
                            codetext.value = 1;
                            timer.cancel();
                            butdisabled.value = false;
                          }

                          // if(timer.tick == 60) timer.cancel(); // 执行5次后停止
                        });
                      }
                    });
                  } else {
                    showTost('输入手机号');
                  }
                },
                child: Obx(() => butdisabled.value
                    ? Text('已发送$codetext',
                        style:
                            TextStyle(fontSize: 12, color: Color(0XFF0A72D9)))
                    : Text('发送验证码',
                        style:
                            TextStyle(fontSize: 12, color: Color(0XFF0A72D9)))),
              ),
            ],
          )),
      Container(
        margin: EdgeInsets.symmetric(horizontal: 30, vertical: 50),
        padding: EdgeInsets.symmetric(vertical: 10),
        alignment: Alignment.center,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular((30)),
          gradient: LinearGradient(
            colors: [
              Color(0xff2189E7),
              Color(0xffBF67F8),
            ], // 定义颜色列表
            begin: Alignment.topLeft, // 渐变开始的位置
            end: Alignment.bottomRight, // 渐变结束的位置
          ),
        ),
        child: Text('确认',
            style: TextStyle(fontSize: 16, color: Color(0xffffffff))),
      ),
    ],
  );
}

Widget setPass(type) {
  return Column(
    crossAxisAlignment: CrossAxisAlignment.start,
    children: [
      Text(
        type == '1' ? '修改登录密码' : '修改支付密码',
        style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
      ),
      SizedBox(height: 5),
      Text('请输入验证码并创建新密码'),
      Container(
        margin: EdgeInsets.only(top: 30),
        padding: EdgeInsets.only(left: 20),
        decoration: BoxDecoration(
          color: Color(0xfff5f5f5),
          borderRadius: BorderRadius.circular(30.0),
          border: Border.all(color: Color(0xffffffff)),
        ),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Expanded(
              child: TextField(
                 enabled:false,
                controller: phone,
                style: TextStyle(
                  fontSize: 12.0, // 字体大小
                ),
                decoration: InputDecoration(
                  hintText: '请输入号码',
                  border: InputBorder.none,
                  hintStyle: TextStyle(fontSize: 12, color: Color(0xff999999)),
                ),
              ),
            ),
          ],
        ),
      ),
      Container(
        margin: EdgeInsets.only(top: 15),
        padding: EdgeInsets.only(left: 20),
        decoration: BoxDecoration(
          color: Color(0xfff5f5f5),
          borderRadius: BorderRadius.circular(30.0),
          border: Border.all(color: Color(0xffffffff)),
        ),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Expanded(
              child: TextField(
                controller: code,
                style: TextStyle(
                  fontSize: 12.0, // 字体大小
                ),
                decoration: InputDecoration(
                  hintText: '请输入验证码',
                  border: InputBorder.none,
                  hintStyle: TextStyle(fontSize: 12, color: Color(0xff999999)),
                ),
              ),
            ),
            TextButton(
              onPressed: () {
                if (phone.text.isNotEmpty) {
                  postData(url: '/phone-code', data: {
                    "phone": phone.text,
                    "value": type == '1' ? "password" : "level2password",
                  }).then((res) {
                    if (res != null) {
                      showTost('发送成功');
                      butdisabled.value = true;
                      codetext.value = 60;

                      Timer.periodic(Duration(seconds: 1), (timer) {
                        codetext.value--;
                        if (codetext.value < 1) {
                          codetext.value = 1;
                          timer.cancel();
                          butdisabled.value = false;
                        }

                        // if(timer.tick == 60) timer.cancel(); // 执行5次后停止
                      });
                    }
                  });
                } else {
                  showTost('输入手机号');
                }
              },
              style: TextButton.styleFrom(padding: EdgeInsets.only(right: 20)),
              child: Obx(() => butdisabled.value
                  ? Text('已发送$codetext',
                      style: TextStyle(fontSize: 12, color: Color(0XFF0A72D9)))
                  : Text('发送验证码',
                      style:
                          TextStyle(fontSize: 12, color: Color(0XFF0A72D9)))),
            ),
          ],
        ),
      ),
      Container(
        margin: EdgeInsets.only(top: 15),
        padding: EdgeInsets.only(left: 20),
        decoration: BoxDecoration(
          color: Color(0xfff5f5f5),
          borderRadius: BorderRadius.circular(30.0),
          border: Border.all(color: Color(0xffffffff)),
        ),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Expanded(
              child: Obx(() => TextField(
                    controller: controller,
                    obscureText: _isObscureds.value,
                    style: TextStyle(
                      fontSize: 12.0, // 字体大小
                    ),
                    decoration: InputDecoration(
                      contentPadding: EdgeInsets.symmetric(vertical: 16),
                      suffixIcon: IconButton(
                        iconSize: 20,
                        padding: EdgeInsets.only(right: 20),
                        constraints: BoxConstraints(),
                        icon: Icon(
                          _isObscureds.value
                              ? Icons.visibility_off
                              : Icons.visibility,
                          color: Colors.grey[600], // 图标颜色
                        ),
                        onPressed: () {
                          _isObscureds.value = !_isObscureds.value;
                        },
                      ),
                      hintText: '请输入新密码',
                      border: InputBorder.none,
                      hintStyle:
                          TextStyle(fontSize: 12, color: Color(0xff999999)),
                    ),
                  )),
            ),
          ],
        ),
      ),
      Container(
        margin: EdgeInsets.only(top: 15),
        padding: EdgeInsets.only(left: 20),
        decoration: BoxDecoration(
          color: Color(0xfff5f5f5),
          borderRadius: BorderRadius.circular(30.0),
          border: Border.all(color: Color(0xffffffff)),
        ),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Expanded(
              child: Obx(() => TextField(
                    controller: controller1,
                    obscureText: _isObscured.value,
                    style: TextStyle(
                      fontSize: 12.0, // 字体大小
                    ),
                    decoration: InputDecoration(
                      contentPadding: EdgeInsets.symmetric(vertical: 16),
                      suffixIcon: IconButton(
                        iconSize: 20,
                        padding: EdgeInsets.only(right: 20),
                        constraints: BoxConstraints(),
                        icon: Icon(
                          _isObscured.value
                              ? Icons.visibility_off
                              : Icons.visibility,
                          color: Colors.grey[600], // 图标颜色
                        ),
                        onPressed: () {
                          _isObscured.value = !_isObscured.value;
                        },
                      ),
                      hintText: '请输入确认密码',
                      border: InputBorder.none,
                      hintStyle:
                          TextStyle(fontSize: 12, color: Color(0xff999999)),
                    ),
                  )),
            ),
          ],
        ),
      ),
      GestureDetector(
        onTap: () {
          if (phone.text.isEmpty) {
           showTost('输入手机号');
            return;
          }
          if (code.text.isEmpty) {
            showTost('输入验证码');
            return;
          }
          if (controller.text.length < 5) {
            showTost('密码长度过低');
            return;
          }
          if (controller.text.isEmpty) {
           showTost('输入密码');
            return;
          }
          if (controller1.text != controller.text) {
           showTost('密码不一致');
            return;
          }
          if (type == '1') {
            postData(url: '/user/password', data: {
              "password": controller.text,
              "password_2": controller1.text,
              "phone_code": code.text,
            }).then((res) {
              if (res != null) {
                showTost('修改成功');
                Get.offNamed('/login/login_type');
              }
            });
          } else {
            postData(url: '/user/level2password', data: {
              "level2password": controller.text,
              "level2password_2": controller1.text,
              "phone_code": code.text,
            }).then((res) {
              if (res != null) {
                showTost('修改成功');
                Get.offNamed('/');
              }
            });
          }
        },
        child: Container(
          margin: EdgeInsets.symmetric(horizontal: 30, vertical: 50),
          padding: EdgeInsets.symmetric(vertical: 10),
          alignment: Alignment.center,
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular((30)),
            gradient: LinearGradient(
              colors: [
                Color(0xff2189E7),
                Color(0xffBF67F8),
              ], // 定义颜色列表
              begin: Alignment.topLeft, // 渐变开始的位置
              end: Alignment.bottomRight, // 渐变结束的位置
            ),
          ),
          child: Text('确认修改',
              style: TextStyle(fontSize: 16, color: Color(0xffffffff))),
        ),
      ),
    ],
  );
}
